package com.yqg.education.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;

import com.yqg.education.service.IVersionService;
import com.yqg.education.entity.Version;

import org.springframework.web.bind.annotation.RestController;

/**
 * VersionController 前端控制器
 *
 * @author zxf
 * @since 2025-09-15
 */
@RestController
@RequestMapping("/version")
public class VersionController {

    @Resource
    private IVersionService versionService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody Version version) {
        return Result.success(versionService.saveOrUpdate(version));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Version version = versionService.getById(id);
        return Result.success(versionService.saveOrUpdate(version));
    }

    /**
     * 查询所有
     */
    @GetMapping("/getVersion")
    public Result getVersion(@RequestParam String version) {
        List<Version> list = versionService.list();
        HashMap<String, Object> map = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            Version latestVersion = list.get(0);
            String newVersionStr = latestVersion.getVersion();

            // 移除版本号前面的'v'前缀（如果有）
            String cleanVersion = version.startsWith("v") ? version.substring(1) : version;
            String cleanNewVersion = newVersionStr.startsWith("v") ? newVersionStr.substring(1) : newVersionStr;

            // 比较版本号
            int comparison = compareVersions(cleanVersion, cleanNewVersion);

            if (comparison < 0) {
                // 当前版本小于最新版本，需要更新
                map.put("isUpdate", true);
                map.put("url", latestVersion.getUrl());
                return Result.success(map);
            } else {
                // 当前版本等于或大于最新版本，不需要更新
                map.put("isUpdate", false);
                return Result.success(map);
            }
        } else {
            map.put("isUpdate", false);
            return Result.success(map);
        }
    }

    // 版本号比较工具方法
    private int compareVersions(String version1, String version2) {
        String[] v1Parts = version1.split("\\.");
        String[] v2Parts = version2.split("\\.");

        int length = Math.max(v1Parts.length, v2Parts.length);

        for (int i = 0; i < length; i++) {
            int v1 = (i < v1Parts.length) ? Integer.parseInt(v1Parts[i]) : 0;
            int v2 = (i < v2Parts.length) ? Integer.parseInt(v2Parts[i]) : 0;

            if (v1 != v2) {
                return v1 - v2;
            }
        }

        return 0; // 版本号完全相同
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(versionService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Version> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(versionService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

